package com.net.xpay.admin.controller.admin;

import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.CheckCodeVo;
import com.net.xpay.common.domain.CheckCode;
import com.net.xpay.common.manager.CheckCodeManager;
import com.net.xpay.core.model.CheckCodeSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/check-code")
@Api(tags = "后台/验证码", description = "后台 验证码")
public class CheckCodeController {

    @Autowired
    private CheckCodeManager checkCodeManager;

    @ApiOperation("搜索验证码")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'check-code::search')")
    public ResultPagedList<CheckCodeVo> searchCheckCode(@RequestBody CheckCodeSearchModel searchModel) {
        List<CheckCode> records = checkCodeManager.search(searchModel);
        long count = checkCodeManager.searchCount(searchModel);

        List<CheckCodeVo> vos = BeanCopierUtil.batchCopy(CheckCode.class, CheckCodeVo.class, records);

        return new ResultPagedList<>(vos, count, searchModel);
    }
}
